import { piniaPersistConfig, HOME_URL } from "@/config";
import router from "@/routers";
import type { TabsParams } from "@/typings";
import { defineStore } from "pinia";
import { ref } from "vue";
import { useKeepAliveStore } from "./keepAlive";

const key: string = "useTabsStore";

export const useTabsStore = defineStore(
  key,
  () => {
    const keepAliveStore = useKeepAliveStore();
    /**
     * @description 标签页列表
     */
    const tabsMenuList = ref<TabsParams[]>([]);

    /**
     * @description 添加标签页
     * @param tab 标签项
     */
    function addTab(tab: TabsParams) {
      if (tabsMenuList.value.every((item) => item.path !== tab.path)) {
        tabsMenuList.value.push(tab);
      }
    }

    /**
     * @description 关闭单个标签页
     * @param tabPath 标签页路径
     * @param isCurrent 是否为当前项
     */
    function closeTab(tabPath: string, isCurrent: boolean = true) {
      if (isCurrent) {
        tabsMenuList.value.forEach((item, index) => {
          if (item.path !== tabPath) return;
          const nextTab = tabsMenuList.value[index + 1] || tabsMenuList.value[index - 1];
          if (!nextTab) return;
          router.push(nextTab.path);
        });
      }
      tabsMenuList.value = tabsMenuList.value.filter((item) => item.path !== tabPath);
    }

    /**
     * @description 关闭侧边标签
     * @param path 标签页路径
     * @param side 侧边
     */
    function closeTabsOnSide(path: string, side: "left" | "right") {
      const currentIndex = tabsMenuList.value.findIndex((item) => item.path === path);
      if (currentIndex !== -1) {
        const renge = side === "left" ? [0, currentIndex] : [currentIndex + 1, tabsMenuList.value.length];
        tabsMenuList.value = tabsMenuList.value.filter((item, index) => index < renge[0] || index >= renge[1] || !item.close);
      }
      keepAliveStore.setKeepAliveName(tabsMenuList.value.map((item) => item.name));
    }

    /**
     * @description 关闭其他标签页
     * @param path 标签页路径
     */
    function closeOtherTabs(path: string) {
      tabsMenuList.value = tabsMenuList.value.filter((item) => item.path === path || !item.close);
      keepAliveStore.setKeepAliveName(tabsMenuList.value.map((item) => item.name));
    }

    /**
     * @description 关闭所有标签页
     */
    function closeAllTabs() {
      tabsMenuList.value = tabsMenuList.value.filter((item) => !item.close);
      keepAliveStore.setKeepAliveName(tabsMenuList.value.map((item) => item.name));
      router.push(HOME_URL);
    }

    /**
     * @description 关闭多个标签页
     * @param tabsMenuValue 不关闭的标签
     */
    function closeMultipleTab(tabsMenuValue?: string) {
      tabsMenuList.value = tabsMenuList.value.filter((item) => item.path === tabsMenuValue || !item.close);
      keepAliveStore.setKeepAliveName(tabsMenuList.value.map((item) => item.name));
    }

    /**
     * @description 设置标签页
     * @param list 标签页列表
     */
    function setTabsMenuList(list: TabsParams[]) {
      tabsMenuList.value = list;
    }

    return {
      tabsMenuList,
      addTab,
      closeTab,
      closeTabsOnSide,
      closeOtherTabs,
      closeAllTabs,
      setTabsMenuList,
      closeMultipleTab
    };
  },
  {
    persist: piniaPersistConfig(key, sessionStorage, ["tabsMenuList"])
  }
);
